package cn.bestick.cblog.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bestick.cblog.dmo.FriendLink;
import cn.bestick.cblog.service.FriendLinkService;
import cn.bestick.cblog.service.impl.FriendLinkServiceImpl;
import cn.bestick.cblog.util.StringUtil;

public class FriendLinkManageServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private FriendLinkService friendLinkService = new FriendLinkServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");

		if ("preSave".equals(action)) {
			preSave(request, response);
		} else if ("save".equals(action)) {
			save(request, response);
		} else if ("delete".equals(action)) {
			delete(request, response);
		} else {
			list(request, response);
		}
	}

	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<FriendLink> friendLinks = friendLinkService.getFriendLinks();
		request.setAttribute("friendLinks", friendLinks);
		request.setAttribute("mainPage", "template/friendLink/manage-list.jsp");
		request.getRequestDispatcher("manage.jsp").forward(request, response);
	}

	private void preSave(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String friendLinkId = request.getParameter("friendLinkId");
		if (StringUtil.isNotEmpty(friendLinkId)) {
			FriendLink friendLink = friendLinkService.getFriendLink(Integer
					.parseInt(friendLinkId));
			request.setAttribute("friendLink", friendLink);
		}
		request.setAttribute("mainPage", "template/friendLink/manage-save.jsp");
		request.getRequestDispatcher("manage.jsp").forward(request, response);
	}

	private void save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String friendLinkId = request.getParameter("friendLinkId");
		String title = request.getParameter("friendLinkTitle");
		String url = request.getParameter("friendLinkUrl");
		FriendLink friendLink = new FriendLink();
		friendLink.setTitle(title);
		friendLink.setUrl(url);
		if (StringUtil.isNotEmpty(friendLinkId)) {
			friendLink.setId(Integer.parseInt(friendLinkId));
		}
		if (StringUtil.isNotEmpty(friendLinkId)) {
			friendLinkService.updateFriendLink(friendLink);
		} else {
			friendLinkService.saveFriendlink(friendLink);
		}
		request.getRequestDispatcher("friendLinkManage?action=list").forward(
				request, response);

	}

	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String friendLinkId = request.getParameter("friendLinkId");

		friendLinkService.deleteFriendLink(Integer.parseInt(friendLinkId));

		request.getRequestDispatcher("friendLinkManage?action=list").forward(
				request, response);
	}

}
